home *** CD-ROM | disk | FTP | other *** search
/ American Osteopathic Ass…tion Yearbook 2005 & 2006 / American Osteopathic Association Yearbook 2005 & 2006.iso / mac / app / atexit.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2004-07-22  |  2.3 KB  |  58 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. '''
  5. atexit.py - allow programmer to define multiple exit functions to be executed
  6. upon normal program termination.
  7.  
  8. One public function, register, is defined.
  9. '''
  10. __all__ = [
  11.     'register']
  12. _exithandlers = []
  13.  
  14. def _run_exitfuncs():
  15.     '''run any registered exit functions
  16.  
  17.     _exithandlers is traversed in reverse order so functions are executed
  18.     last in, first out.
  19.     '''
  20.     while _exithandlers:
  21.         (func, targs, kargs) = _exithandlers.pop()
  22.         func(*targs, **kargs)
  23.  
  24.  
  25. def register(func, *targs, **kargs):
  26.     '''register a function to be executed upon normal program termination
  27.  
  28.     func - function to be called at exit
  29.     targs - optional arguments to pass to func
  30.     kargs - optional keyword arguments to pass to func
  31.     '''
  32.     _exithandlers.append((func, targs, kargs))
  33.  
  34. import sys
  35. if hasattr(sys, 'exitfunc'):
  36.     register(sys.exitfunc)
  37.  
  38. sys.exitfunc = _run_exitfuncs
  39. del sys
  40. if __name__ == '__main__':
  41.     
  42.     def x1():
  43.         print 'running x1'
  44.  
  45.     
  46.     def x2(n):
  47.         print 'running x2(%s)' % `n`
  48.  
  49.     
  50.     def x3(n, kwd = None):
  51.         print 'running x3(%s, kwd=%s)' % (`n`, `kwd`)
  52.  
  53.     register(x1)
  54.     register(x2, 12)
  55.     register(x3, 5, 'bar')
  56.     register(x3, 'no kwd args')
  57.  
  58.